import $ from 'dfish-widget';
import { defineAsyncComponent, resolveComponent } from 'vue';
import { baseURL } from '@/config';
export function dialog(template, props, options, target) {
  return $.vm(target).pop(template, props, options);
}

/**
 * @param {String|Component|Array} com  对话框的名称，或导入的对话框组件
 * @example 如果对话框是全局组件，可传入对话框的name：
 * const testDialog = asyncDialog('TestDialog')
 * @example 如果对话框非全局组件，需要先导入：
 * import TestDialog from './TestDialog.vue'
 * const testDialog = asyncDialog(TestDialog)
 * @example 动态导入的方式:
 * const testDialog = asyncDialog(() => import('./TestDialog.vue'))
 * const data = await testDialog(props)  // TestDialog内部调用 dialog.value.resolve(data) 返回数据
 */
export function asyncDialog(com, options, target) {
  if (typeof com === 'string') {
    com = resolveComponent(com);
  } else if (typeof com === 'function') {
    com = defineAsyncComponent(com);
  }
  return (props) => dialog(com, props, options, target);
}

function getWidgetRange(target, range) {
  const widget = Array.isArray(target) ? $.vm() : $(target);
  if (!range && !(target instanceof window.Event)) {
    range = target;
  }
  return { widget, range };
}

/**
 * 获取表单数据
 * @param target Event|Component|[Component Array]
 * @example 根据点击事件获取表单数据
 * const data = await getPostData(event)
 * @example 获取指定组件内的表单数据
 * const data = await getPostData([formLayout.value])
 */
export function getPostData(target, com) {
  const { widget, range } = getWidgetRange(target, com);
  return widget.submit((data) => data, { range });
}

/**
 * 判断表单是否有修改
 * @param target Event|Component|[Component Array]
 * @example 根据点击事件判断表单是否有修改
 * const result = isModified(event)
 * @example 获取指定组件内的表单是否有修改
 * const result = isModified([formLayout.value])
 */
export function isModified(target) {
  const { widget, range } = getWidgetRange(target);
  return $.vm(widget).isModified(range);
}

/**
 * 把表单的当前值设定为默认值
 * @param target Event|Component|[Component Array]
 * @example 根据点击事件保存表单默认值
 * saveModified(event)
 * @example 保存指定组件内的表单默认值
 * saveModified([formLayout.value])
 */
export function saveModified(target) {
  const { widget, range } = getWidgetRange(target);
  $.vm(widget).saveModified(range);
}
export function getCurDateTime() {
  const curTime = $.dateFormat(new Date(), 'yyyy-MM-dd HH:mm:ss');
  return curTime;
}
export function getCurDate() {
  const curDate = $.dateFormat(new Date(), 'yyyy-MM-dd');
  return curDate;
}
export function getCurMonth() {
  const curMonth = $.dateFormat(new Date(), 'yyyy-MM');
  return curMonth;
}
export function getCurYear() {
  const curYear = $.dateFormat(new Date(), 'yyyy');
  return curYear;
}

export function showInfo(text, position = 'br') {
  dialog('w-alert', {
    title: '操作提示',
    text,
    position,
    timeout: 3000,
    class: 'app-alert'
  });
}

export function showWarn(text, yes) {
  dialog('w-alert', {
    title: '操作提示',
    icon: '.f-i .f-i-alert-warning',
    text,
    position: 'c',
    class: 'app-alert',
    yes
  });
}

export function showConfirm(text, yes, no) {
  return dialog('w-confirm', {
    title: '操作提示',
    text,
    cover: true,
    class: 'app-confirm',
    yes,
    no
  });
}

export function showTip(target, text) {
  $(target).pop('w-tip', { text, class: 'common-tip', maxWidth: 520 }, { mode: 'single' });
}

export const requireImg = (imgName) => {
  return new URL(`/src/assets/images/${imgName}`, import.meta.url).href;
};

export const requireHttpImg = (path) => {
  return `${baseURL}${path}`;
};

export const parseImg = (imgString = '{}') => {
  const imgInfo = JSON.parse(imgString);
  return requireHttpImg(`affair/file/inline/${imgInfo?.id}`);
};
